package cn.tsmchina.one.controller.rproduct;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.tsmchina.one.domain.activity.Activity;
import cn.tsmchina.one.domain.category.Category;
import cn.tsmchina.one.domain.menu.Menu;
import cn.tsmchina.one.domain.rproduct.Rcatagory;
import cn.tsmchina.one.domain.rproduct.Rproduct;
import cn.tsmchina.one.service.rcatagory.RcatagoryService;
import cn.tsmchina.one.service.rproduct.RproductService;
import cn.tsmchina.one.util.C;
import cn.tsmchina.one.util.JsonUtil;
import cn.tsmchina.one.util.LoginUtil;
import cn.tsmchina.one.util.Return;

@Controller
@RequestMapping("/rcatagory")
public class RcatagoryControler {
	@Autowired
	private RcatagoryService rcatagoryservice;
	
	@Autowired
	private RproductService rproductservice;	
	
	@RequestMapping("/query")
	public String queryOrder(HttpServletRequest request)
	{
		int page = 1;
		try
		{
			String pageStr = request.getParameter("page");
			if(!StringUtils.isEmpty(pageStr))
				page = Integer.parseInt(pageStr);
			//long cid = LoginUtil.getCustomerId(request, customerService);
	    	List<Rcatagory>	list =rcatagoryservice.getRcatagoris(page);
	    	int total = rcatagoryservice.total();
	    	//int total = list.size();
	    	int pageTotal = total % C.Paging.SIZE == 0 ? total / C.Paging.SIZE : total / C.Paging.SIZE + 1;
	    	request.setAttribute("page", page);
	    	request.setAttribute("total", total);
	    	request.setAttribute("pageTotal", pageTotal);
	    	request.setAttribute("rcatagory", list);
	    	request.setAttribute("len", list.size());
		} catch (Exception e)
		{
			request.setAttribute("len", 0);
		}
		return "rcatagory/queryRcatagory";
	}
	
	@RequestMapping("/add")
	public String add(HttpServletRequest request)
	{
		List<Rcatagory>  categories = this.rcatagoryservice.getAllRcatagoris();
		return "rcatagory/addRcatagory";
	}
	
	@RequestMapping(value="/add",method= RequestMethod.POST)
	public String addActivity(@ModelAttribute Rcatagory rcatagory,HttpServletRequest request)
	{
		try
		{	
//			long cid = LoginUtil.getCustomerId(request);
//			activity.setCustomerId(cid);
			rcatagoryservice.addRcatagory(rcatagory);
			request.setAttribute("msgType", C.Msg.OK);
			request.setAttribute("msg", "添加成功!");
			request.setAttribute("return",request.getContextPath()+"/rcatagory/query");
		} catch (Exception e)
		{
			request.setAttribute("msgType", C.Msg.ERROR);
			request.setAttribute("msg", "添加失败!");
			request.setAttribute("return",request.getContextPath()+"/rcatagory/query");
		}
		
		return Return.redirect(request);
	}
	
	/**
	 * 分类修改
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public String editProduct(@PathVariable("id") int id,HttpServletRequest request)
	{
		Rcatagory product = rcatagoryservice.getProduct(id);
		//List<Menu> list = menuService.getAllMenu();
		request.setAttribute("p", product);
		//request.setAttribute("menus", list);
		request.setAttribute("returnUrl",Return.encode(request.getHeader("referer")));
		
		//l//ong cid = LoginUtil.getCustomerId(request, customerService);
		//List<Category> categories = categoryService.getAllSubRestaurant(cid);
		//request.setAttribute("categories", categories);
		//List<Category> foods = categoryService.getAllFoodCategory("0000", cid);//对于本应用，每个店铺的菜品种类肯定是一致的，故为了后台在添加菜品时避免二级目录联动，这里做简化处理
		//request.setAttribute("foods", foods);
		
		//int categoryId = product.getCategory();
		//String categorySid = categoryService.getById(categoryId).getCid();
		//request.setAttribute("cid", categorySid.substring(0, 4));
		//request.setAttribute("fid", "0000"+categorySid.substring(4));
		return "rcatagory/editRcatagory";
	}
	
	@RequestMapping(value="/edit",method = RequestMethod.POST)
	public String editProduct(@ModelAttribute Rcatagory product,HttpServletRequest request)
	{
		try
		{
			//String categoryCid = categories+foods.substring(4);
			//long customerId = LoginUtil.getCustomerId(request, customerService);
			//int categoryId = categoryService.getByCategoryCid(categoryCid, customerId).getId();
			//product.setCategory(categoryId);//只取二级目录id
			
			/*通过id找出相应Rcategory下的所有商品，并修改name字段*/
			int rid = product.getId();
			String newName = product.getCatagoryName();
			String oldName = rcatagoryservice.getProduct(rid).getCatagoryName();
			rcatagoryservice.updateRcatagory(product);
			if(!newName.equals(oldName)){
				System.out.println("修改分类名称，将修改rproduct下的所有category字段");
				List<Rproduct> l = rproductservice.getProducts(0);
				int i = 0;
				for(Rproduct rp : l){
					if(rp.getCategory().equals(oldName)){
						rp.setCategory(newName);
						rproductservice.updateProduct(rp);
						i++;
					}
				}
				System.out.println("修改" + i +"个红商品的分类名称。");
				
				
			}
			
			
			
			request.setAttribute("msgType", C.Msg.OK);
			request.setAttribute("msg", "修改成功!");
			request.setAttribute("return", request.getParameter("return"));
		}catch (Exception e)
		{
			request.setAttribute("msgType", C.Msg.ERROR);
			request.setAttribute("msg", "修改失败!");
			request.setAttribute("return", request.getParameter("return"));
		}
		return Return.redirect(request);
	}
	
	/**
	 * 删除产品
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/delete/{id}")
	public String deleteProduct(@PathVariable("id") int id, HttpServletRequest request)
	{
		
		try
		{
			rcatagoryservice.deleteRcatagoryById(id);
			request.setAttribute("msgType", C.Msg.OK);
			request.setAttribute("msg", "删除成功!");
			request.setAttribute("return", request.getHeader("referer"));
		} catch (Exception e)
		{
			request.setAttribute("msgType", C.Msg.ERROR);
			request.setAttribute("msg", "删除失败!");
			request.setAttribute("return", request.getHeader("referer"));
		}
		return Return.redirect(request);
	}
}
